package com.shinow.capacitor.plusins.decard.f11;

import com.getcapacitor.JSObject;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SocialSecurityCard extends JSObject {
	public static String BasicsDataRegExp = "__CardIssuingPlaceNo|__SocialSecurityNumber|__SocialSecurityCardNumber|__CardIdentificationCode|__Name|__CardResetInformation|__CardCanonicalVersion|__IssuingDate|__ExpirationDate|__TerminalNumber|__TerminalEquipmentNumber|";

	public SocialSecurityCard(String Data) {
		this.regExpMatchMergeData(Data, BasicsDataRegExp);
	}

	public SocialSecurityCard(String Data, String RegExp) {
		super();
		this.regExpMatchMergeData(Data, RegExp);
	}

	public void regExpMatchMergeData(String Data, String RegExp) {
		ArrayList<String> RegExpFields = new ArrayList<String>();
		Pattern RegExpPattern = Pattern.compile("(__[^\\|]*)");
		Pattern DataPattern = Pattern.compile(RegExp.replaceAll("__[^\\|]+", "([^|]*)").replaceAll("\\|", "\\\\|").replaceAll("\\$", "\\\\\\$"));
		Matcher RegExpMatcher = RegExpPattern.matcher(RegExp);
		Matcher DataMatcher = DataPattern.matcher(Data);

		while (RegExpMatcher.find()) {
			RegExpFields.add(RegExpMatcher.group().replace("__", ""));
		}

		if (DataMatcher.find() && DataMatcher.groupCount() == RegExpFields.size()) {
			for (int i = 0, length = DataMatcher.groupCount(); i < length; i++) {
				this.put(RegExpFields.get(i), DataMatcher.group(i + 1));
			}
		}
	}

	public SocialSecurityCard conversionFlag() {
		if (this.has("NameExpand") && !"".equals(this.getString("NameExpand"))) {
			this.put("Name", this.getString("Name") + this.getString("NameExpand"));
		}

		if (this.has("CardClassFlag")) {
			this.put("CardClass", SocialSecurityCardFlagContrast.CardClass.getString(this.getString("CardClassFlag"), ""));
		}

		if (this.has("SexFlag")) {
			this.put("Sex", SocialSecurityCardFlagContrast.Sex.getString(this.getString("SexFlag"), ""));
		}

		if (this.has("NationFlag")) {
			this.put("Nation", SocialSecurityCardFlagContrast.Nation.getString(this.getString("NationFlag"), ""));
		}

		if (this.has("BirthPlaceFlag")) {
			this.put("BirthPlace", SocialSecurityCardFlagContrast.BirthPlace.getString(this.getString("BirthPlaceFlag"), ""));
		}

		return this;
	}
}
